package custom
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.ClassFactory;
	
	/** 
	 *  DataGrid that uses checkboxes for multiple selection
	 */
	public class CheckBoxDataGrid extends DataGrid
	{
		[Bindable]
		public var isCheckBox:Boolean = true;
		[Bindable]
		public var hasNumber:Boolean=true;
		//固定序号labelFunction函数  
		private function RowNum(oItem:Object,iCol:int):String  
		{
			var iIndex:int = dataProvider.getItemIndex(oItem) + 1;  
			return String(iIndex);  	
		} 
		//内嵌checkboxdatagird
		override public function set columns(value:Array):void{
			var newValue:Array = new Array();
			
			if(isCheckBox) {
				
				var cbGrid:DataGridColumn =new DataGridColumn();
				cbGrid.width=20;
				cbGrid.sortable=false;
				cbGrid.itemRenderer=new ClassFactory(CheckBoxRender);
				//cbGrid.headerRenderer=new ClassFactory(CheckBoxHeader);
				
				newValue.push(cbGrid);
			}
			
			if(hasNumber){
				var cbGrid1:DataGridColumn =new DataGridColumn();
				cbGrid1.labelFunction=RowNum;
				cbGrid1.sortable=false;
				cbGrid1.width=40;
				cbGrid1.dataField="orderId";
				cbGrid1.headerText="序号";
				newValue.push(cbGrid1);
			}
			
			while(value.length>0){
				var temp:DataGridColumn=value.shift() as DataGridColumn;
				temp.setStyle("textAlign","center");
				newValue.push(temp);
			}
			//		value.push(cbGrid);
			super.columns=newValue;
		}
		override protected function selectItem(item:IListItemRenderer,
											   shiftKey:Boolean, ctrlKey:Boolean,
											   transition:Boolean = true):Boolean
		{
			// only run selection code if a checkbox was hit and always
			// pretend we're using ctrl selection
			if(isCheckBox) {
				if (item is CheckBox)
					return super.selectItem(item, false, true, transition);
				return false;
			}
			return false;
		}
		
		// turn off selection indicator
		override protected function drawSelectionIndicator(
			indicator:Sprite, x:Number, y:Number,
			width:Number, height:Number, color:uint,
			itemRenderer:IListItemRenderer):void
		{
			//if(!(itemRenderer is CheckBox))
			super.drawSelectionIndicator(indicator,x,y,width,height,color,itemRenderer);
		}
		
		// whenever we draw the renderer, make sure we re-eval the checked state
		override protected function drawItem(item:IListItemRenderer,
											 selected:Boolean = false,
											 highlighted:Boolean = false,
											 caret:Boolean = false,
											 transition:Boolean = false):void
		{
			//		if(isCheckBox) {
			//			CheckBox(item).invalidateProperties();
			//		}
			super.drawItem(item, selected, highlighted, caret, transition);
		}
		
		// fake all keyboard interaction as if it had the ctrl key down
		override protected function keyDownHandler(event:KeyboardEvent):void
		{
			// this is technically illegal, but works
			event.ctrlKey = true;
			event.shiftKey = false;
			super.keyDownHandler(event);
		}
		
		
	}
	
}